package cn.green.captcha.config;

import cn.green.captcha.property.CaptchaProperty;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 图形验证码组件配置器
 */
@Configuration
public class CaptchaConfig {

    @Autowired
    private CaptchaProperty property;

    /**
     * 向容器中注入图形验证码生成器
     * @return      图形验证码生成器
     */
    @Bean
    public Producer captcha() {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", property.getImageWidth());
        properties.setProperty("kaptcha.image.height", property.getImageHeight());
        properties.setProperty("kaptcha.textproducer.char.string", property.getCharSet());
        properties.setProperty("kaptcha.textproducer.char.length", property.getLength());
        //配置
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

}
